package com.zhejiangzhengyuan.municipal_common.utils.sms;

import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageV2Request;
import com.baidubce.services.sms.model.SendMessageV2Response;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author ZhouXiang
 * @since on 2019/12/25
 */
public class SendMessage {
  private static String endPoint = "http://sms.bj.baidubce.com"; // SMS服务域名，可根据环境选择具体域名
  private static String accessKeyId = "3411004b648f4fff804c60bb5f13165b"; // 发送账号安全认证的Access Key ID
  private static String secretAccessKy =
      "ac7ed944a3c242dabcbe261e3d8b621f"; // 发送账号安全认证的Secret Access Key
  private static String invokeId = "ypTzFnYj-uyqW-R7D9"; // 浙江正元的签名
  private static SmsClient smsClient = null;

  public static String sendErrorMessage(
      String phoneNumber, String systemName, String deviceCode, String warnMessage, String action) {
    if (Objects.isNull(smsClient)) {
      // 配置config
      SmsClientConfiguration config = new SmsClientConfiguration();
      config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKy));
      config.setEndpoint(endPoint);
      // 实例化发送客户端
      smsClient = new SmsClient(config);
    }
    String templateCode = "smsTpl:cd7bf1fd-fc77-4a54-b24a-8df02a51f182"; // 告警模板Code
    // 定义请求参数

    Map<String, String> vars = new HashMap<>(); // 若模板内容为：您的验证码是${code},在${time}分钟内输入有效
    vars.put("sn_1", systemName);
    vars.put("sn_2", deviceCode);
    vars.put("sn_3", warnMessage);
    vars.put("sn_4", action);

    // 实例化请求对象
    SendMessageV2Request request = new SendMessageV2Request();
    request
        .withInvokeId(invokeId)
        .withPhoneNumber(phoneNumber)
        .withTemplateCode(templateCode)
        .withContentVar(vars);

    // 发送请求
    SendMessageV2Response response = smsClient.sendMessage(request);

    // 解析请求响应 response.isSuccess()为true 表示成功
    return response.isSuccess() ? null : response.getMessage();
  }

  public static String sendTaskMessage(
      String phoneNumber, String systemName, String taskType, String action) {
    if (Objects.isNull(smsClient)) {
      // 配置config
      SmsClientConfiguration config = new SmsClientConfiguration();
      config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKy));
      config.setEndpoint(endPoint);
      // 实例化发送客户端
      smsClient = new SmsClient(config);
    }
    String templateCode = "smsTpl:b117f4e0-2515-4a0c-8ca7-9a90785502e5"; // 任务派发模板Code
    // 定义请求参数

    Map<String, String> vars = new HashMap<>(); // 若模板内容为：您的验证码是${code},在${time}分钟内输入有效
    vars.put("sn_1", systemName);
    vars.put("sn_2", taskType);
    vars.put("sn_3", action);

    // 实例化请求对象
    SendMessageV2Request request = new SendMessageV2Request();
    request
        .withInvokeId(invokeId)
        .withPhoneNumber(phoneNumber)
        .withTemplateCode(templateCode)
        .withContentVar(vars);

    // 发送请求
    SendMessageV2Response response = smsClient.sendMessage(request);

    // 解析请求响应 response.isSuccess()为true 表示成功
    return response.isSuccess() ? null : response.getMessage();
  }

  public static String sendTomorrowTaskMessage(
      String phoneNumber, String systemName, String taskType, String action, String startTime) {
    if (Objects.isNull(smsClient)) {
      // 配置config
      SmsClientConfiguration config = new SmsClientConfiguration();
      config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKy));
      config.setEndpoint(endPoint);
      // 实例化发送客户端
      smsClient = new SmsClient(config);
    }
    String templateCode = "smsTpl:eb5cf59e-b5ae-4b51-940e-7369ded6062b"; // 任务派发模板Code
    // 定义请求参数

    Map<String, String> vars = new HashMap<>();
    vars.put("sn_1", systemName);
    vars.put("sn_2", taskType);
    vars.put("sn_3", startTime);
    vars.put("sn_4", action);

    // 实例化请求对象
    SendMessageV2Request request = new SendMessageV2Request();
    request
        .withInvokeId(invokeId)
        .withPhoneNumber(phoneNumber)
        .withTemplateCode(templateCode)
        .withContentVar(vars);

    // 发送请求
    SendMessageV2Response response = smsClient.sendMessage(request);

    // 解析请求响应 response.isSuccess()为true 表示成功
    return response.isSuccess() ? null : response.getMessage();
  }
}
